package com.shou.infrastructure.persistent.dao;

import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * ClassName:IBaseRuleDao
 * Project:yvt-rule
 * Package: com.sunlight.infrastructure.dao
 * Description
 *
 * @Author Nemo
 * @Create 2024/12/25 15:36
 * @Version 1.0
 */
@Mapper
public interface IBaseDao {

    @Delete("<script>" +
            "DELETE FROM ${tableName} " +
            "WHERE id IN " +
            "<foreach item='id' index='index' collection='ids' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    void deleteByIds(@Param("ids") List<Integer> ids, @Param("tableName") String tableName);

    @Select("SELECT EXISTS(SELECT 1 FROM ${tableName} WHERE id = #{id})")
    boolean checkIfExistById(@Param("id") Integer id, @Param("tableName") String tableName);

    @Select("select IFNULL((select id from ${tableName} limit 1), 0)")
    boolean checkIfRecordsExist(@Param("tableName") String tableName);

    @Delete("truncate table ${tableName}")
    void deleteAllByTableName(@Param("tableName") String tableName);

    @Select("<script>" +
            "SELECT EXISTS(" +
            "    SELECT 1 FROM ${tableName} WHERE code = #{code} " +
            "    <if test='excludeIds != null and excludeIds.length > 0'>" +
            "        AND id NOT IN " +
            "        <foreach item='id' index='index' collection='excludeIds' open='(' separator=',' close=')'>" +
            "            #{id}" +
            "        </foreach>" +
            "    </if>" +
            ")" +
            "</script>")
    boolean checkIfExistByCode(String code, String tableName, Integer[] excludeIds);

    @Select("SELECT COLUMN_NAME " +
            "FROM INFORMATION_SCHEMA.COLUMNS " +
            "WHERE TABLE_SCHEMA = 'shou' " +
            "AND TABLE_NAME  = #{enTableName};")
    List<String> selectColumnsByEnTableName(String enTableName);

    @Select("<script>" +
            "SELECT EXISTS(" +
            "    SELECT 1 FROM ${enTableName} WHERE name = #{name} " +
            "    <if test='excludeIds != null and excludeIds.length > 0'>" +
            "        AND id NOT IN " +
            "        <foreach item='id' index='index' collection='excludeIds' open='(' separator=',' close=')'>" +
            "            #{id}" +
            "        </foreach>" +
            "    </if>" +
            ")" +
            "</script>")
    boolean checkIfExistByName(String name, String enTableName, Integer[] excludeIds);

    @Select("<script>" +
            "SELECT EXISTS(" +
            "    SELECT 1 FROM ${tableName} WHERE code = #{code} AND lot_code = #{lotCode}" +
            "    <if test='excludeIds != null and excludeIds.length > 0'>" +
            "        AND id NOT IN " +
            "        <foreach item='id' index='index' collection='excludeIds' open='(' separator=',' close=')'>" +
            "            #{id}" +
            "        </foreach>" +
            "    </if>" +
            ")" +
            "</script>")
    boolean checkIfSpaceExistByCode(String code, String tableName, Integer[] excludeIds, String lotCode);

    @Select("<script>" +
            "    SELECT COUNT(*) FROM ${enTableName} WHERE user_id = #{userId}" +
            "    <if test='excludeIds != null and excludeIds.length > 0'>" +
            "        AND id NOT IN " +
            "        <foreach item='id' index='index' collection='excludeIds' open='(' separator=',' close=')'>" +
            "            #{id}" +
            "        </foreach>" +
            "    </if>" +
            "</script>")
    Integer checkIfExistByReservedUserId(Integer userId, String enTableName, Integer[] excludeIds);

    @Select("<script>" +
            "SELECT EXISTS(" +
            "    SELECT 1 FROM ${enTableName} WHERE code = #{spaceCode} AND is_available = 1" +
            "    <if test='excludeIds != null and excludeIds.length > 0'>" +
            "        AND id NOT IN " +
            "        <foreach item='id' index='index' collection='excludeIds' open='(' separator=',' close=')'>" +
            "            #{id}" +
            "        </foreach>" +
            "    </if>" +
            ")" +
            "</script>")
    boolean checkIfAvailableBySpaceCode(String spaceCode, String enTableName, Integer[] excludeIds);
}
